package com.lw.extraceim.ui.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.lw.extraceim.R;
import com.lw.extraceim.app.ExtraceApplication;
import com.lw.extraceim.loader.PackageLoader;
import com.lw.extraceim.model.TransPackage;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.ui.activity.base.BaseActivity;
import com.lw.extraceim.ui.activity.tp.PackageInfoActivity;
import com.lw.extraceim.ui.activity.tp.UnpackageInfoActivity;
import com.lw.extraceim.ui.adapter.TransPackageAdapter;
import com.lw.extraceim.ui.fragments.base.BaseFragment;
import com.lw.extraceim.util.JsonUtils;
import com.lw.extraceim.zxing.ExtraceCaptureActivity;

import org.json.JSONException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : 拆包
 * @Date : 2019/5/15 18:35
 */
public class UnPackingFragment extends BaseFragment {
    private static final String TAG = "UnPackingFragment";
    private Button mButtonScan;
    private ListView mLvUnpackList;
    private TransPackageAdapter adapter;

    private List<TransPackage> transPackageList;

    public UnPackingFragment(Context mContext, ExtraceApplication application) {
        super(mContext, application);
    }

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.layout_unpack, null);
        mButtonScan = view.findViewById(R.id.btn_scan);
        mLvUnpackList = view.findViewById(R.id.listview_unpack);
        return view;
    }

    @Override
    public void initData() {
        transPackageList = new ArrayList<>();
        loadUnpckedPackage();
    }

    @Override
    public void initEvent() {
        mButtonScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BaseActivity) getActivity()).startQRScan(getActivity(), ExtraceCaptureActivity.class, "扫描条形码", ExtraceCaptureActivity.SCAN_UNPACK_PACKAGE);
            }
        });

        mLvUnpackList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PackageInfoActivity.actionStart(getContext(), transPackageList.get(position));
            }
        });
    }

    @Override
    public void refreshUI() {
       refreshPackageList();
    }

    /**
     * 获取已经拆包的快件信息
     */
    public void loadUnpckedPackage() {
        PackageLoader loader = new PackageLoader(getContext());
        loader.getPackageListByUserId(application.getUserInfo().getuId(), 3, 6, new HttpResponseParamCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                alert("网络错误" + e.getMessage());
            }

            @Override
            public void onResponse(HttpResponseParam response, int id) {
                Log.d(TAG, "onResponse: " + response.reseponseJson);
                if (response.code.getCode() == ResponseCode.SUCESS) {
                    try {
                        transPackageList = JsonUtils.fromJson(response.reseponseJson.getJSONArray("packageList").toString(), new TypeToken<List<TransPackage>>() {
                        });
                        refreshPackageList();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    alert(response.code.getMessage());
                }
            }
        });
    }

    /**
     * 刷新包裹列表
     */
    public void refreshPackageList() {
        mLvUnpackList.setAdapter(new TransPackageAdapter(transPackageList, getContext()));
    }

    @Override
    public void onResume() {
        loadUnpckedPackage();
        super.onResume();
    }
}
